//
//  AddViewController.m
//  通讯录限时123123
//
//  Created by dllo on 15/12/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AddViewController.h"
#import "Student.h"

@interface AddViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UITextField *textF1;
@property (nonatomic, retain) UITextField *textF2;
@property (nonatomic, retain) UITextField *textF3;
@property (nonatomic, retain) UITextField *textF4;

@end

@implementation AddViewController

- (void)dealloc
{
    [_imageV release];
    [_textF1 release];
    [_textF2 release];
    [_textF3 release];
    [_textF4 release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"添加联系人";
    
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 80, 150)];
    self.imageV.image = [UIImage imageNamed:@"image6.jpg"];
    self.imageV.userInteractionEnabled = YES;
    [self.view addSubview:self.imageV];
    [_imageV release];
    
    
    self.textF1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 100, 80, 30)];
    self.textF1.placeholder = @"姓名";
    self.textF1.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.textF1];
    [_textF1 release];
    
    self.textF2 = [[UITextField alloc] initWithFrame:CGRectMake(120, 150, 50, 30)];
    self.textF2.placeholder = @"性别";
    self.textF2.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.textF2];
    [_textF2 release];
    
    self.textF3 = [[UITextField alloc] initWithFrame:CGRectMake(230, 150, 50, 30)];
    self.textF3.placeholder = @"年龄";
    self.textF3.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.textF3];
    [_textF3 release];
    
    self.textF4 = [[UITextField alloc] initWithFrame:CGRectMake(120, 220, 150, 30)];
    self.textF4.placeholder = @"电话";
    self.textF4.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.textF4];
    [_textF4 release];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.imageV addGestureRecognizer:tap];
    [tap release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];

}

- (void)buttonAction:(UIBarButtonItem *)sender
{
    Student *stu = [[Student alloc] init];
    stu.name = self.textF1.text;
    stu.sex = self.textF2.text;
    stu.age = self.textF3.text;
    stu.phone = self.textF4.text;
    stu.image = self.imageV.image;
    
    [self.delegate getStu:stu];
    
    [stu release];
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.imageV.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
    UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
    imagePC.delegate = self;
    
    [self presentViewController:imagePC animated:YES completion:^{
        
        
    }];
    [imagePC release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
